<html>
  <body>
    <form action="" method="post">
<?php
//header("Content-type: text/plain");


/**
 * http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid
 * http://www.php.net/manual/en/function.uniqid.php#94959
 * http://club.shelek.ru/viewart.php?id=307#post_classic
 */



require_once('settings.php');
require_once('../experiments/lib.dbg.php');
require_once('recaptcha/recaptchalib.php');


# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;


if (array_key_exists('recaptcha_response_field', $_POST)) {
        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER['REMOTE_ADDR'],
                                        $_POST['recaptcha_challenge_field'],
                                        $_POST['recaptcha_response_field']);

        if ($resp->is_valid) {
                echo "You got it!";
        } else {
                # set the error code so that we can display it
                $error = $resp->error;
        }
} else {
    xdbg()->info($GLOBALS);
    exit(0);
}

var_dump($resp);
echo recaptcha_get_html($recaptcha_publickey, $error);

/**
 * Generate UUID
 */
function gen_uuid() {
    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}

/**
 * Generate SuperKey
 */
function gen_superkey() {
    // 0: 97-122 (a-z)
    // 1: 65-90 (A-Z)
    // 2: 48-57 (0-9)

    $skey = '';    // character we generate

    for ($i=0; $i<8; $i++) {    // TODO: use constant for super key length
        $groupe = mt_rand(0, 2);

        if ($groupe > 1) {    // 2: 48-57 (0-9)
            $c = mt_rand(48, 57);
        } elseif ($groupe > 0) {     // 1: 65-90 (A-Z)
            $c = mt_rand(65, 90);
        } else {    // 0: 97-122 (a-z)
            $c = mt_rand(97, 122);
        }
        $skey .= chr($c);
    }

    return $skey;
}

/**
 * Send back error and stop execution
 * @param array $error	- array data
 */
function send_error($error) {
    xdbg()->err($error);
    exit(0);
}


try {
    $GLOBALS['#dbh'] = new PDO($dsn, $user, $password, $options);

    /**
     * Sugar to get access to database object
     * @return PDO
     */
    function dbh() {
        return $GLOBALS['#dbh'];
    }

} catch (PDOException $e) {
    send_error(array('Connection failed', $e->getMessage()));
}



//json_decode($json);
//json_encode($value, $options);


















?>
<br/>
    <input type="submit" value="submit" />
    </form>